<template>
	<view class="container">
		<view class="main">
			<view class="mobile">
				<text>点击发送验证码到您的手机</text>
				<text>{{ userInfo.user.phonenumber.replace(userInfo.user.phonenumber.slice(3,7), '****')}}</text>
			</view>
			<view class="verifyCode">
				<input type="number" v-model="value"/>
				<!-- 发送验证码 -->
				<view v-if="!verificationFlag" class="no-send" @click="handleSendCode">发送验证码</view>
				<view v-if="verificationFlag" style="color: #a2a2a2;">已发送（{{ count }}S）</view>
			</view>
		</view>
		<view class="button" @click="handleComfirmCode">下一步</view>
			
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				userInfo: { user: {}, params: {} },//当前用户信息
				value: '',//输入的验证码
				timer: null,//验证码计时器
				count: 60,//验证码计时时间
				verificationFlag: false, // 是否发送过验证码
				checkCode: '',//验证码
				path: '',//设置完密码回哪里
			}
		},
		methods: {
			//设置导航标题
			changTitle() {
				if(this.codeType == 1) {
					this.headerTitle = '修改支付密码'
				}else if(this.codeType == 2) {
					this.headerTitle = '重置支付密码'
				}
			},
			// 发送验证码 
			async handleSendCode () {
				this.verificationFlag = true
				let res = await this.$fetch(this.$api.send_pay_code, {}, 'POST')
				if (res.code) return uni.showToast({ title: res.msg, icon: 'none' })
				this.timer = setInterval(() => {
				  if (this.count === 0) {
				    clearInterval(this.timer)
					this.verificationFlag = false
					this.count = 60
				  }
				  this.count = --this.count
				}, 1000)
			},
			// 下一步确认密码
			async handleComfirmCode () {
				this.checkCode = this.value
				console.log('验证码', this.value)
				let res = await this.$fetch(this.$api.check_pay_pwd, { checkCode: this.value }, 'POST')
				if (res.code ) return uni.showToast({ title: res.msg, icon: 'none' })
				uni.redirectTo({ url: '/pages/comfirm-pay-code/comfirm-pay-code?path=' + this.path})
			},
		},
		onLoad(options) {
			this.path = options.path
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
	}
</script>

<style lang="less">
.container {
	& > .main{
		padding: 30rpx;
		margin-top: 30rpx;
		margin-bottom: 140rpx;
		background-color: #FFFFFF;
		.mobile {
			height: 40rpx;
			margin-bottom: 46rpx;
			text:first-child {
				color: rgba(64,33,16,0.50);
				margin-right: 30rpx;
			}
			text:last-child {
				font-weight: bold;
			}
		}
		.verifyCode {
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #F2F2F2;
			border-radius: 2px;
			padding: 0 30rpx;
			.no-send {
				color: #F39B19;
			}
			input {
				flex: 1;
			}
		}
	}
	& > .button {
		width: 690rpx;
		margin: 0 auto;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FCC928;
		&:active {
			opacity: 0.7;
		}
	}
}
</style>
